package com.wuyb.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wuyb.mapper.RoleMapper;
import com.wuyb.mapper.UserRoleMapper;
import com.wuyb.pojo.Role;
import com.wuyb.pojo.UserRole;
import com.wuyb.pojo.entity.BaseResponse;
import com.wuyb.pojo.entity.ResultUtils;
import com.wuyb.service.RoleService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 *
 * @Author: wyb
 * @Date: 2024/01/02/19:05
 * @Description:
 */

@Service
@Slf4j
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements RoleService {

    @Resource
    private RoleMapper roleMapper;

    @Resource
    private UserRoleMapper userRoleMapper;

    @Override
    public List<Role> selectRoleWithUserId(Integer id) {
        return roleMapper.selectRoleWithUserid(id);
    }

    @Override
    public BaseResponse<?> selectRoleWithoutHidden(Integer userId) {
        return ResultUtils.success(roleMapper.selectRoleWithoutHidden(userId));
    }

    @Override
    public BaseResponse<?> updateRole(Integer userId, Map<Integer, Boolean> roleParam) {
        roleParam.forEach((k,v) -> {
            userRoleMapper.delete(new QueryWrapper<UserRole>()
                    .eq("user_id_xq",userId)
                    .eq("role_id_xq",k));
            if (v) {
                UserRole userRole = new UserRole().setUserId(userId).setRoleId(k);
                userRoleMapper.insert(userRole);
            }
        });
        return ResultUtils.success("权限修改成功！");
    }

    @Override
    public BaseResponse<?> selectAllRole() {
        return ResultUtils.success(roleMapper.selectList(new QueryWrapper<Role>().eq("hidden_xq",0)));
    }
}
